﻿@model ManageCommentsViewModel

@{
    ViewBag.Title = Strings.Comment_Moderate;
}

@section head {
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnSelectComments").click(function () {
                $(".chkComment").each(function () {
                    $(this).attr("checked", true);
                });
            });

            $("#btnClearComments").click(function () {
                $(".chkComment").each(function () {
                    $(this).attr("checked", false);
                });
            });

            function SetCommentStatus(commentStatus) {
                if ($(".chkComment:checked").size() == 0) return;

                $.post("/Comment/UpdateStatus", { keys: $(".chkComment:checked").val(), status: commentStatus }, function (obj) {
                    if (obj.Success == false) {
                        alert(obj.Message);
                    }
                    else {
                        $(".chkComment:checked").each(function () {
                            var status = "";
                            if (commentStatus == 1)
                                status = "Pending";
                            if (commentStatus == 2)
                                status = "Approved";

                            $(this).parents("tr td:eq(1)").text(status);
                        });
                    }
                });
            }

            $("#btnDeleteComments").click(function () {
                if ($(".chkComment:checked").size() == 0) return;

                var result = confirm("Are you sure you want to delete the selected comments? This action is not reversible.");
                if (result == false) return;

                $(".chkComment:checked").each(function () {
                    var row = $(this).closest("tr");
                    var id = $(this).val();
                    $.post("/Comment/Delete", { commentID: id }, function (obj) {
                        if (obj.Success == true)
                            row.remove();
                        else {
                            alert(obj.Message);
                        }
                    });
                });
            });

            $("#btnApproveComments").click(function () {
                SetCommentStatus(2);
            });
        });
    </script>
}
@using (Html.BeginForm())
{         
    <fieldset class="shaded">
        <legend>@Strings.Comment_Moderate</legend>
        @foreach (var item in Model.Statuses)
        { 
            @Html.RadioButton("SelectedStatus", item.Value, Convert.ToInt32(item.Value) == Model.SelectedStatus) 
            @item.Text
        }
        <br />
        <label>@Strings.Search</label>
        @Html.TextBoxFor(x => x.Query)
        <input type="submit" value='@Strings.Search' />
        @(Html.Grid(Model.Data).Columns(column =>
    {
        column.For(x => String.Format("<input type='checkbox' class='chkComment' value='{0}' id='commentIds' name='commentIds' />", x.PostCommentId)).DoNotEncode();
        column.For(x => Html.RouteLink(x.Post.Title, "ViewPost", new { slug = x.Post.Slug })).Named("Post").DoNotEncode();
        column.For(x => x.CommentStatus).Sortable(false);
        column.For(x => x.Author);
        column.For(x => x.IpAddress);
        column.For(x => x.DateTime).Attributes(@nowrap => "nowrap");
        column.For(x => Utils.GetSummary(x.Content, 200)).Named("Summary");
    }).Sort(Model.GridSortOptions))
        @Html.Pager(Model.PagingInfo)
        <br />
        <input type="button" id="btnSelectComments" value='@Strings.Select' />
        <input type="button" id="btnClearComments" value='@Strings.Clear' />
        <input type="button" id="btnApproveComments" value='@Strings.Approve' />
        <input type="button" id="btnDeleteComments" value='@Strings.Delete' />
    </fieldset>
}